package com.example.hui.case04_16110100814;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter{

    private List<Article> list;
    private Context context;
    private LayoutInflater inflater;

    public MyAdapter(Context context){
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<Article> list) {
        this.list = list;
    }



    @Override
    public int getItemCount() {
        return list.size();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item,viewGroup,false);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewholder, int i) {
        Article article = list.get(i);
        if (article == null) return;
        MyViewHolder viewHolder = (MyViewHolder) viewholder;
        viewHolder.title.setText(article.getTitle());

    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        public MyViewHolder(View itemView)
        { super(itemView);
            title = itemView.findViewById(R.id.textView);
        }
    }


}
